#include "gfaux_tools.h"
#include "sample_log.h"
#include "CImgWrapper.h"
using namespace net::gdface::sdk;
using namespace gdface::log;
void gdface::ui::showText(const std::vector<CodeInfo>& codes, const std::string& title_prefix) {
	SAMPLE_OUT("{}find face:{}", title_prefix, codes.size());
	for (int i = 0; i < codes.size(); ++i) {
		auto pos = codes[i].__get_pos();
		if (pos) {
			SAMPLE_OUT("{} {}", i, *pos);
		}		
	}
}
//template struct cimg_library::CImg<uint8_t>;
//template class cimg_library::CImgWrapper<uint8_t>;

typedef cimg_library::CImg<uint8_t> CImgUChar;
typedef cimg_library::CImgWrapper<uint8_t> CImgWrapperUChar;

void draw_FRect(CImgUChar &cimg, const FRect&rect) {
	static uint8_t color[] = { 0,255,255 };
	static uint8_t text_color[] = { 255,0,0 };
	cimg.draw_rectangle(rect.__get_left(), rect.__get_top(), rect.__get_left() + rect.__get_width() - 1, rect.__get_top() + rect.__get_height() - 1, color, 1.0f, ~0U);
	auto title = sample_format("X{},Y{},W{},H{}", rect.__get_left(), rect.__get_top(), rect.__get_width(), rect.__get_height());
	cimg.draw_text(rect.__get_left() + 1, rect.__get_top() + 1, title.c_str(), text_color);
}
void gdface::ui::showImage(const std::vector<CodeInfo>& codes, const std::string& imgData, const std::string& title_prefix) {
	CImgWrapperUChar image_jpg;
	image_jpg.load_mem_jpeg((uint8_t *)imgData.c_str(), imgData.size());
	// 在图像上划人脸位置矩形框
	static uint8_t color[] = { 0,255,255 };
	for (auto code : codes) {
		auto rect = code.__get_pos();
		if (rect) {
			draw_FRect(image_jpg, *rect);
		}			
	}

	auto title = sample_format("{}find face:{}", title_prefix, codes.size());
	image_jpg.draw_text(0, image_jpg.height() - 20, "press ESC to continue...", color, 0, 1.0f, 20);

	image_jpg.display(title.c_str());
}
void gdface::ui::showImage2In1(const std::string& leftimgData, const std::string& rightimgData, double similarity, const std::string& title_prefix, const FRect* leftrect, const FRect* rightrect) {
	CImgWrapperUChar left_jpg;
	left_jpg.load_mem_jpeg((uint8_t *)leftimgData.c_str(), leftimgData.size());
	CImgWrapperUChar right_jpg;
	right_jpg.load_mem_jpeg((uint8_t *)rightimgData.c_str(), rightimgData.size());
	if (nullptr != leftrect) {
		draw_FRect(left_jpg, *leftrect);
	}
	if (nullptr != rightrect) {
		draw_FRect(right_jpg, *rightrect);
	}
	auto width = left_jpg.width() + right_jpg.width();
	auto height = std::max(left_jpg.height(), right_jpg.height());
	CImgUChar uimg(width, height, 1, 3);
	uimg.draw_image(0, (height - left_jpg.height()) / 2, left_jpg, 1.0f);
	uimg.draw_image(left_jpg.width(), (height - right_jpg.height()) / 2, right_jpg, 1.0f);
	static uint8_t foreground_color[] = { 0,0,255 };
	uimg.draw_text(10, 10, sample_format("similarity:{}", similarity).c_str(), foreground_color, 0, 1.0f, 20);
	uimg.draw_text(0, uimg.height() - 20, "press ESC to continue...", foreground_color, 0, 1.0f, 20);

	auto title = sample_format("{}similarity:{}", title_prefix, similarity);
	uimg.display(title.c_str());
}